package com.cdg.process;

import javax.annotation.Resource;

import com.cdg.process.common.ProcessConstants.ProcessStatus;
import com.cdg.process.common.ProcessConstants.ProcessType;
import com.cdg.process.factory.ProcessServiceFactory;
import com.cdg.process.service.ProcessService;


public class BizProcessBoImpl implements BizProcessBo {


    @Resource
    private ProcessServiceFactory processServiceFactory;

    @Override
    public void refreshProcess(Long settlementId, ProcessType processType) {
        ProcessService processService = processServiceFactory.getProcessService(processType);
        ProcessStatus processStatus = processService.getProcessStatus(settlementId);
        System.out.println(processStatus);
//        if (processStatus.name().equals(processDo.getProcessStatus())) {
//            return;
//        }
        //设置到对应表的状态字段中
//        processDo.setProcessStatus(processStatus.name());
        //保存入库
        //save();
    }

    @Override
    public void createProcesses(Long settlementId, boolean isNeedBillable) {
        short stage = 1;
        //有几个流程链路就一起创建
        doCreateProcess(settlementId, ProcessType.MY_1, stage++);
        doCreateProcess(settlementId, ProcessType.MY_2, stage++);
        doCreateProcess(settlementId, ProcessType.MY_3, stage);
    }

    private void doCreateProcess(Long settlementId, ProcessType processType, short stage) {
//        SettlementProcessDo settlementProcessDo = new SettlementProcessDo();
//        settlementProcessDo.setSettlementId(settlementId);
//        settlementProcessDo.setProcessStatus(ProcessStatus.NEW.name());
//        settlementProcessDo.setProcessType(processType.name());
//        settlementProcessDo.setStage(stage);
//        settlementProcessDoMapperExt.insertSelective(settlementProcessDo);
    }

    @Override
    public void autoStartProcess(Long settlementId, ProcessType processType) {
//        SettlementProcessDo processDo =
//            settlementProcessDoMapperExt.lockProcessBySettlementIdAndType(settlementId, processType.name());
//        if (!ProcessStatus.NEW.name().equals(processDo.getProcessStatus())) {
//            return;
//        }
        ProcessService processService = processServiceFactory.getProcessService(processType);
        processService.processAutoStart(settlementId);
        refreshProcess(settlementId, processType);
    }

}
